#!/bin/bash

# For all script types, returning an exit code of 0 (success) means the
# script execution completed successfully.
#
# Requirements scripts can have the following exit codes that
# influence how the Client will handle the Fileset:
#
# - 210: This exit code will cause the Fileset to be treated like the
#        installation was successful (unless another requirements script fails,)
#        but the Fileset will not be downloaded nor installed.
#
# - 220: This exit code will prevent the installation and cause the client to
#        stop retrying unless a manual action is made (verify, reinstall, etc.)
#        or the Fileset is updated.
#
# Returning any other exit code but 0 (e.g. 1 or -1) will be reported as a
# "Requirements Failure: Script" in the Client Info window and Fileset Report.
# This will also prevent the contents of the Fileset from downloading and
# installing. In this case, requirements scripts will be executed every 2
# minutes and the Fileset will be installed when they all return 0.
#
# For other types of scripts, any non-zero exit code (e.g. 1 or -1) will cause the
# Fileset installation to fail and a script failure to be reported.
#
# If the script finishes without returning an exit code, the exit code 0
# (success) is assumed by default.
#
# Add the contents of your script below:
#!/bin/zsh

# Determine OS version
osvers=$(sw_vers -productVersion)

# Check to see if macOS is 11 or greater
if [ ${osvers%%.*} -ge 11 ]; then
  # Check processor type
  processor=$(/usr/bin/uname -p)
  if [[ "$processor" != "arm" ]]; then
    echo "$(date) processor: ${processor}. No need to install Rosetta."
    exit 210
  else
    # Check if Rosetta process is running and instal if not found,
    # perform a non-interactive install of Rosetta.
    if [[ ! $(/usr/bin/pgrep -x oahd) ]]; then
        /usr/sbin/softwareupdate --install-rosetta --agree-to-license
        if [[ $? -eq 0 ]]; then
                echo "$(date) Rosetta has been successfully installed."
        else
                echo "$(date) Rosetta installation failed!"
                exitcode=1
	     exit 1
        fi
    else
        echo "$(date) Rosetta already installed. Nothing to do."
        exit 210
    fi
  fi
  else
    echo "$(date) Mac is running macOS $osvers"    
    echo "$(date) No need to install Rosetta on this version of macOS."
    exit 1
fi 

exit 0
